package com.msmall.community.bbs.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.exception.AuthorityException;
import com.msmall.base.exception.DataNotFoundException;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.base.util.Constants;
import com.msmall.community.bbs.dao.ThreadDao;
import com.msmall.community.bbs.model.Master;
import com.msmall.community.bbs.model.Thread;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.MasterManager;
import com.msmall.community.bbs.service.ThreadManager;
import com.msmall.community.bbs.util.BbsUtil;
import com.msmall.community.common.util.FilterCharsUtil;
import com.msmall.community.score.service.ScoreManager;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;

public class ThreadManagerImpl extends BaseManagerImpl<Thread, Long> implements ThreadManager{

	private static final Log logger = LogFactory.getLog(ThreadManagerImpl.class);
	
	private ThreadDao threadDao;

	private MasterManager masterManager;
	
	private ScoreManager scoreManager;
	
	private UserManager userManager;
	
	public void setThreadDao(ThreadDao threadDao) {
		this.threadDao = threadDao;
	}

	public void setMasterManager(MasterManager masterManager) {
		this.masterManager = masterManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setScoreManager(ScoreManager scoreManager) {
		this.scoreManager = scoreManager;
	}

	public void saveThread(Thread thread, Long boardId, Long topicId,Long userid, String userAgentType) {
		UserInfo user = userManager.getByUserid(userid);
		String content = thread.getContent();
		Date date = new Date();
		String[] urls = new String[]{};
		try {
			urls = BbsUtil.writeForumFile(content, boardId, date, userAgentType);
		} catch (IOException e) {
			logger.debug(e.getStackTrace());
		}
		thread.setFilePath(urls[0]);
		thread.setFileName(urls[1]);
		thread.setFileExt(urls[2]);
		thread.setFileFlag(Constants.FILE_CREATED_FLAG_WML);
		
		thread.setSource(Constants.USER_AGENT_TYPE_WML.equalsIgnoreCase(userAgentType)? ConstantDB.USER_AGENT_TYPE_WML :ConstantDB.USER_AGENT_TYPE_HTML);
		thread.setCreateTime(date);
		thread.setEnabled(ConstantDB.AVALIABLE);
		thread.setSignature("");

		Topic topic = (Topic) threadDao.get(Topic.class, topicId);
		topic.setThreadAmount(topic.getThreadAmount() + 1);
		topic.setLastThreadTime(date);
		
		thread.setTopicid(topic.getTopicId());
		thread.setUserid(userid);
		threadDao.save(thread);
		
		scoreManager.addThread(userid, false);
		userManager.addThreadAmount(userid);
		
	}

	public void delete(Long threadId, Long userid) throws AuthorityException, DataNotFoundException {
		Thread thread = threadDao.get(threadId);
		List list = masterManager.findMasterByThreadId(threadId);
		Iterator it = list.iterator();
		boolean tag = false; // 是否有删除权限
		while(it.hasNext()){
			Master master = (Master) it.next();
			if(userid.compareTo(master.getUserid()) == 0){
				thread.setEnabled((short)0);
				threadDao.save(thread);
				tag =true;
				break;
			}
		}
		if(tag){
			scoreManager.reduceThread(thread.getUserid(), true);
		}else{
			logger.error("user : " + userid + " don't have enouth authority to delete thread :" + threadId);
			throw new AuthorityException("user : " + userid + " don't have enouth authority to delete thread :" + threadId);
		}
	}
	
	/**
	 * 验证thread content
	 * @return 1:正常；2字符太少；3字符太多;4含有非法字符
	 */
	public int validateThreadContent(String content){
		int titleOK = BbsUtil.checkThreadContentLength(content);
		if(titleOK == 2){
			return 2;
		}
		if(titleOK == 3){
			return 3;
		}
		boolean titleC = FilterCharsUtil.containsFilterChars(content);
		if(titleC){
			return 4;
		}
		return 1;
	}
}
